package com.yanxinim.cn.store.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yanxinim.cn.R;
import com.yanxinim.cn.store.bean.ProductType;

import java.util.ArrayList;
import java.util.List;

public class TextAdapter extends RecyclerView.Adapter<TextAdapter.ViewHolder> {
    private Context context;
    private List<String> dataList = new ArrayList<>();
    private ItemOnClickListener itemOnClickListener;
    private int selectedPosition = -1;

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public TextAdapter(Context context, List<String> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void addAllData(List<String> dataListUpdate) {
        if (dataListUpdate != null && dataListUpdate.size() > 0) {
            dataList.addAll(dataListUpdate);
            notifyItemRangeChanged(dataList.size() - dataListUpdate.size(), dataListUpdate.size());
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.new_text, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {

        if (selectedPosition == position){
            viewHolder.text.setTag("main_color");
            viewHolder.text.setTextColor(context.getResources().getColor(R.color.main_color));
        }else {
            viewHolder.text.setTag("text_color");
            viewHolder.text.setTextColor(context.getResources().getColor(R.color.text_color));
        }
        viewHolder.text.setText(dataList.get(position));
        viewHolder.text.setBackgroundResource(R.drawable.a_gray_line_bg);
        viewHolder.text.setTag("text_color");
        viewHolder.text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (String.valueOf(viewHolder.text.getTag()).equals("main_color")) {
                    selectedPosition = -1;
                    viewHolder.text.setTag("text_color");
                    viewHolder.text.setTextColor(context.getResources().getColor(R.color.text_color));
                } else {
                    selectedPosition = position;
                    viewHolder.text.setTag("main_color");
                    viewHolder.text.setTextColor(context.getResources().getColor(R.color.main_color));
                }

            }
        });
        viewHolder.itemView.setOnClickListener(v -> itemOnClickListener.onItemClick(dataList.get(position),position));
    }

    public void updateItem(int position){
        if (selectedPosition != position){
            selectedPosition = position;
            notifyItemChanged(position);
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.tv_text);
        }
    }

    public interface ItemOnClickListener {
        void onItemClick(String string, int position);
    }
}
